package org.harryrose.resultkit.converters;

import java.util.HashSet;

import org.harryrose.resultkit.exceptions.TypeException;

public class DefaultNumericDataConverter implements NumericDataConverter{

	@Override
	public Number convert(Object object) throws TypeException {
		if(!isNumberType(object))
		{
			throw new TypeException("The default numeric data converter can only process numeric types.");
		}
		return (Number) object;
	}

	private boolean isNumberType(Object variable)
	{
		HashSet<Class<?>> admissableClasses = new HashSet<Class<?>>();
		admissableClasses.add(int.class);
		admissableClasses.add(double.class);
		admissableClasses.add(long.class);
		admissableClasses.add(float.class);
		
		Class<?> type = null;
		type = variable.getClass();
		return Number.class.isAssignableFrom(type) || admissableClasses.contains(type);
	}
}
